
	# Menu Convertor
	
	class MenuConvertor
		def self.process bundle_path
				bundle_name = bundle_path.scan( /([^\/]+\/)+([^\.]+)(\.itBundle)/i )
				if bundle_name.length
					bundle_name = bundle_name[0][ bundle_name[0].length - 2 ];
					puts "Processing `#{bundle_name}`..."
					menu_file_path = bundle_path + "/info.itInfo";
					if FileTest.exist?( menu_file_path )
						file_contents = IO.read( menu_file_path )
						self.find_and_replace( file_contents, bundle_path, bundle_name )
						file = File.new( menu_file_path, "w+" )
						file.write( file_contents )
						file.close
					else
						puts "\tMenu file cannot be found."
					end
				end
		end
		
		def self.find_and_replace str, bundle_path, bundle_name
			# find all item names
			replaces = Array.new
			str.scan( /\{[\s]*(item)[\s]*\:[\s]*"([^\"]+)"[\s*]\}/ ) { |match|
				puts "\t#{match[1]} in #{bundle_path}"
				replace = nil
				if is_snippet( match[1], bundle_path )
					replaces << [ match[1], "bundle.#{bundle_name}.snippet.#{match[1]}" ]
				elsif is_grammar( match[1], bundle_path )
					replaces << [ match[1], "bundle.#{bundle_name}.language.#{match[1]}" ]
				end
=begin				
				if replace
					puts "\t\t#{replace}"
				end
=end				
			}
			
			puts "\tReplacing..."
			replaces.each do |replace|
				str.gsub!( "\"#{replace[0]}\"",  "\"#{replace[1]}\"" )
			end
			puts "\t\t#{replaces.length} replacements made"
		end
		
		def self.is_snippet name, bundle_path
			return FileTest.exist?( "#{bundle_path}/snippets/#{name}.itSnippet" )
		end
		
		def self.is_grammar name, bundle_path
			return FileTest.exist?( "#{bundle_path}/syntaxes/#{name}.itGrammar" )
		end
	end